var _o134;
var cc__decorate = __decorate;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var $2EventCenter = require("EventCenter");
var $2Platformconf = require("Platformconf");
var $2GameMessageBase = require("GameMessageBase");
window.wx;
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var def_WSClient = _o134 = /* */function (_cc$Component32) {
  function _ctor() {
    var _this149;
    babel_classCallCheck(this, _ctor);
    _this149 = _callSuper(this, _ctor, arguments);
    _this149.heartCheckTime = 50;
    _this149.sendlist = [];
    _this149.Reconnect = false;
    _this149.CatchFame = false;
    _this149.MachOvermsg = null;
    _this149.wsUrl = "";
    _this149.roomid = -1;
    return _this149;
  }
  babel_inherits(_ctor, _cc$Component32);
  return babel_createClass(_ctor, [{
    key: "onLoad",
    value: function onLoad() {
      var e = $2Platformconf.default.eqconfig();
      this.wsUrl = "wss://kczl.eqmobi.com/wechat/" + e.EQAPPKEY + "/" + e.EQVERSION + "/ws";
      _o134._instance = this;
      var t = window.location.href.split("=")[1];
      if (undefined !== t) {
        this.roomid = parseInt(t);
        cc.game.addPersistRootNode(this.node);
        this.connectws();
      } else {
        this.node.destroy();
      }
    }
  }, {
    key: "c",
    value: function c() {
      var e = new WebSocket(this.wsUrl);
      this.ws = e;
      return this.ws;
    }
  }, {
    key: "reconnect",
    value: function reconnect() {
      this.ws && 0 == this.ws.readyState || this.connectws();
    }
  }, {
    key: "connectws",
    value: function connectws() {
      var _this150 = this;
      var e = this.c();
      e.onopen = function () {
        _this150.send(new $2GameMessageBase.GameMessageC2S_JoinRoom(_this150.roomid));
        $2EventCenter.default.postEvent("connectws");
        _this150.heartCheckTime = 50;
      };
      e.onerror = function (e) {
        _this150.reconnect();
        console.log("onerror", e);
      };
      e.onclose = function () {
        _this150.reconnect();
      };
      e.onmessage = function (e) {
        _this150.heartCheckTime = 50;
        var t = JSON.parse(e.data);
        if (!(t.msgtype == $2GameMessageBase.GameMessageType.C2S_CreateRoom)) {
          if (t.msgtype == $2GameMessageBase.GameMessageType.C2S_SpeechResult) {
            _this150.send(new $2GameMessageBase.GameMessageC2S_C2S_PlayAudioCtx(t.filename));
          } else if (t.msgtype == $2GameMessageBase.GameMessageType.C2S_OnServerRecordStop) {
            $2EventCenter.default.postEvent("RecordStop", t.text);
          } else {
            t.msgtype == $2GameMessageBase.GameMessageType.C2S_TranslateResult && $2EventCenter.default.postEvent("RecordStop", t.text);
          }
        }
      };
    }
  }, {
    key: "send",
    value: function send(e) {
      console.log("发送消息", e);
      if (this.ws && 1 == this.ws.readyState) {
        this.heartCheckTime = 50;
        this.ws.send(JSON.stringify(e));
      } else {
        this.reconnect();
      }
    }
  }, {
    key: "heartCheck",
    value: function heartCheck(e) {
      if (this.heartCheckTime < 0) {
        this.heartCheckTime = 50;
        var _e83 = new $2GameMessageBase.GameMessageC2S_PING();
        this.send(_e83);
      } else {
        this.heartCheckTime -= e;
      }
    }
  }, {
    key: "update",
    value: function update(e) {
      this.heartCheck(e);
    }
  }], [{
    key: "getInstance",
    value: function getInstance() {
      return _o134._instance;
    }
  }]);
}(cc.Component);
def_WSClient._instance = null;
def_WSClient = _o134 = cc__decorate([ccp_ccclass], def_WSClient);
exports.default = def_WSClient;